package cn.sdjzu.exercise;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;

public class MyView{
	private Context mContext;
	ExerciseApp app;
	int xySize;
	public double[] xValue = new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // 10s就21个点,1个点0.5s。
	public double[] yValue = new double[] { -10, -80, -80, -80, -60, 40, 20,
			10, 10, 10 };
	public int color = Color.BLACK;
	public PointStyle style = PointStyle.POINT;
	private String title = "Air temperature";

	public MyView(Context context) {
		mContext = context;
	}

	public Intent getIntent(ExerciseApp paraApp) {
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(1);
		setRenderer(renderer);

		XYSeriesRenderer r = (XYSeriesRenderer) renderer.getSeriesRendererAt(0);
		r.setLineWidth(30f);

		setChartSettings(renderer, "title-hidden", "x-text-hidden",
				"y-text-hidden", 0, 14, 0, 100, Color.LTGRAY, Color.LTGRAY);

		renderer.setShowGrid(false);
		renderer.setShowLabels(false);
		renderer.setShowLegend(false);
		renderer.setShowAxes(false);
		renderer.setPanEnabled(false, false);
		renderer.setZoomButtonsVisible(false);
		app = (ExerciseApp) ExerciseApp.getInstance();
		app = paraApp;
		XYMultipleSeriesDataset dataset = buildDataset();

		Intent intent = ChartFactory.getCubicLineChartIntent(mContext, dataset,
				renderer, 0.3f);
		return intent;
	}

	
	public GraphicalView getModeView(ExerciseApp paraApp, String mode) {
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(1);
		setRenderer(renderer);
		XYSeriesRenderer r = (XYSeriesRenderer) renderer.getSeriesRendererAt(0);
		r.setLineWidth(30f);
		renderer.setShowGrid(false);
		renderer.setShowLabels(false);
		renderer.setShowLegend(false);
		renderer.setShowAxes(false);
		renderer.setPanEnabled(false, false);
		renderer.setZoomButtonsVisible(false);
		app = (ExerciseApp) ExerciseApp.getInstance();
		app = paraApp;
		// 返回自定义坐标点集合
		XYMultipleSeriesDataset dataset = buildDataset(mode);
		setChartSettings(renderer, "title-hidden", "x-text-hidden",
				"y-text-hidden", 0, xySize-1, 0, 100, Color.LTGRAY,
				Color.LTGRAY);
		// 将数据结合作为参数传递进去
		GraphicalView view = ChartFactory.getCubeLineChartView(mContext,
				dataset, renderer, 0.3f);
		
		return view;
	}

	public GraphicalView getChartView(ExerciseApp paraApp) {
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(1);
		setRenderer(renderer);
		XYSeriesRenderer r = (XYSeriesRenderer) renderer.getSeriesRendererAt(0);
		r.setLineWidth(30f);
		renderer.setShowGrid(false);
		renderer.setShowLabels(false);
		renderer.setShowLegend(false);
		renderer.setShowAxes(false);
		renderer.setPanEnabled(false, false);
		renderer.setZoomButtonsVisible(false);
		app = (ExerciseApp) ExerciseApp.getInstance();
		app = paraApp;
		// 返回自定义坐标点集合
		XYMultipleSeriesDataset dataset = buildDataset();
		setChartSettings(renderer, "title-hidden", "x-text-hidden",
				"y-text-hidden", 0, xySize - 1, 0, 100, Color.LTGRAY,
				Color.LTGRAY);
		// 将数据结合作为参数传递进去
		GraphicalView view = ChartFactory.getCubeLineChartView(mContext,
				dataset, renderer, 0.3f);
		return view;
	}

	public void setChartSettings(XYMultipleSeriesRenderer renderer,
			String title, String xTitle, String yTitle, double xMin,
			double xMax, double yMin, double yMax, int axesColor,
			int labelsColor) {
		renderer.setChartTitle(title);
		renderer.setXTitle(xTitle);
		renderer.setYTitle(yTitle);
		renderer.setXAxisMin(xMin);
		renderer.setXAxisMax(xMax);
		renderer.setYAxisMin(yMin);
		renderer.setYAxisMax(yMax);
		renderer.setAxesColor(axesColor);
		renderer.setLabelsColor(labelsColor);
	}

	private void setRenderer(XYMultipleSeriesRenderer renderer) {
		renderer.setPointSize(5f);
		renderer.setMargins(new int[] { 0, 0, 0, 0 });
		renderer.setChartTitleTextSize(60);
		XYSeriesRenderer r = new XYSeriesRenderer();
		r.setColor(color);
		r.setPointStyle(style);
		r.setLineWidth(30); // dong add
		renderer.addSeriesRenderer(r);
	}

	private XYMultipleSeriesDataset buildDataset() {
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		addXYSeries(dataset);
		return dataset;
	}

	public XYMultipleSeriesDataset buildDataset(String mode) {
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		addModeXYSeries(dataset, mode);
		return dataset;
	}

	private void addModeXYSeries(XYMultipleSeriesDataset dataset, String mode) {
		XYSeries series = new XYSeries(title, 0);
		float iPushTime = 0;
		float iHoldTime = 0;
		float iResetTime = 0;
		float iSleepTime = 0;
		if (mode.equals("add_muscle")) {
			iPushTime = 2;
			iHoldTime = 1;
			iResetTime = (float) 2.5;
			iSleepTime = 1;
		} else if (mode.equals("reduce_fat")) {
			iPushTime = (float) 1.5;
			iHoldTime = (float) 0.5;
			iResetTime = (float) 1.5;
			iSleepTime = (float) 0.5;
		} else if (mode.equals("bodybuilding")) {
			iPushTime = (float) 1.5;
			iHoldTime = (float) 0.5;
			iResetTime = (float) 1.5;
			iSleepTime = (float) 0.5;
		} else if (mode.equals(":add_force")) {
			iPushTime = (float) 1.5;
			iHoldTime = (float) 0.5;
			iResetTime = 2;
			iSleepTime = 1;
		}else if(mode.equals("customize")){
			iPushTime = Float.parseFloat(app.mPushTime);
			iHoldTime = Float.parseFloat(app.mHoldTime);
			iResetTime = Float.parseFloat(app.mResetTime);
			iSleepTime = Float.parseFloat(app.mSleepTime);
		}
		app.mOneTotalTime = (int) ((iPushTime + iHoldTime + iResetTime + iSleepTime) * 2); // 用于判断一次是否移动完毕
		int totalTime = (int) ((iPushTime + iHoldTime + iResetTime + iSleepTime) * 2) + 2; // 为了保持长度，所以乘以2
		xValue = new double[totalTime];
		yValue = new double[totalTime];
		for (int i = 0; i < totalTime; i++) {
			xValue[i] = i; // X轴赋值
		}

		for (int y = 0; y < totalTime; y++) {
			yValue[y] = 10; // X轴赋值
		}
		// 为了与图片的类似，我们把延迟时间的一半分到推之前
		int HalfsleepTime = (int) iSleepTime;
		for (int i = 0; i < HalfsleepTime; i++) {
			yValue[i+1] = 10; // Y轴赋值
		}
		yValue[HalfsleepTime+1] = 10;
		// 押下时间
		int fToiPushTime = (int) (iPushTime * 2);
		for (int i = 1; i < fToiPushTime; i++) {
			yValue[i + HalfsleepTime+1] = 10 + i * (70 / fToiPushTime); // Y轴赋值
		}
		yValue[fToiPushTime + HalfsleepTime+1] = 80;

		// 保持时间
		int fToiHoldTime = (int) (iHoldTime * 2);
		for (int i = 0; i < fToiHoldTime; i++) {
			yValue[fToiPushTime + HalfsleepTime + i+1] = 80; // Y轴赋值
		}
		yValue[fToiPushTime + fToiHoldTime + HalfsleepTime+1] = 80;

		// 放松时间
		int fToiResetTime = (int) (iResetTime * 2);
		for (int i = 1; i <= fToiResetTime; i++) {
			yValue[i + fToiPushTime + fToiHoldTime + HalfsleepTime+1] = 80 - i
					* (70 / fToiResetTime); // Y轴赋值
		}
		int fToiSleepTime = (int) iSleepTime;
		for (int i = 1; i <= fToiSleepTime; i++) {
			yValue[fToiPushTime + fToiHoldTime + fToiResetTime + HalfsleepTime
					+ i+1] = 10; // Y轴赋值
		}
		yValue[fToiPushTime + fToiHoldTime + fToiResetTime + fToiSleepTime
				+ HalfsleepTime+1] = 10;
		double[] xV = xValue;
		double[] yV = yValue;
		xySize = totalTime;
		for (int i = 0; i < totalTime; i++) {
			series.add(xV[i], yV[i]);
		}
		dataset.addSeries(series);
	}

	private void addXYSeries(XYMultipleSeriesDataset dataset) {
		XYSeries series = new XYSeries(title, 0);
		float iPushTime = Float.parseFloat(app.mPushTime);
		float iHoldTime = Float.parseFloat(app.mHoldTime);
		float iResetTime = Float.parseFloat(app.mResetTime);
		float iSleepTime = Float.parseFloat(app.mSleepTime);
		app.mOneTotalTime = (int) ((iPushTime + iHoldTime + iResetTime + iSleepTime) * 2); // 用于判断一次是否移动完毕
		int totalTime = (int) ((iPushTime + iHoldTime + iResetTime + iSleepTime) * 2) + 1; // 为了保持长度，所以乘以2
		xValue = new double[totalTime];
		yValue = new double[totalTime];
		for (int i = 0; i < totalTime; i++) {
			xValue[i] = i; // X轴赋值
		}

		for (int y = 0; y < totalTime; y++) {
			yValue[y] = 10; // X轴赋值
		}
		// 为了与图片的类似，我们把延迟时间的一半分到推之前
		int HalfsleepTime = (int) iSleepTime;
		for (int i = 0; i < HalfsleepTime; i++) {
			yValue[i] = 10; // Y轴赋值
		}
		yValue[HalfsleepTime] = 10;
		// 押下时间
		int fToiPushTime = (int) (iPushTime * 2);
		for (int i = 1; i < fToiPushTime; i++) {
			yValue[i + HalfsleepTime] = 10 + i * (70 / fToiPushTime); // Y轴赋值
		}
		yValue[fToiPushTime + HalfsleepTime] = 80;

		// 保持时间
		int fToiHoldTime = (int) (iHoldTime * 2);
		for (int i = 0; i < fToiHoldTime; i++) {
			yValue[fToiPushTime + HalfsleepTime + i] = 80; // Y轴赋值
		}
		yValue[fToiPushTime + fToiHoldTime + HalfsleepTime] = 80;

		// 放松时间
		int fToiResetTime = (int) (iResetTime * 2);
		for (int i = 1; i <= fToiResetTime; i++) {
			yValue[i + fToiPushTime + fToiHoldTime + HalfsleepTime] = 80 - i
					* (70 / fToiResetTime); // Y轴赋值
		}
		int fToiSleepTime = (int) iSleepTime;
		for (int i = 1; i <= fToiSleepTime; i++) {
			yValue[fToiPushTime + fToiHoldTime + fToiResetTime + HalfsleepTime
					+ i] = 10; // Y轴赋值
		}
		yValue[fToiPushTime + fToiHoldTime + fToiResetTime + fToiSleepTime
				+ HalfsleepTime] = 10;
		double[] xV = xValue;
		double[] yV = yValue;
		xySize = totalTime;
		for (int i = 0; i < totalTime; i++) {
			series.add(xV[i], yV[i]);
		}
		dataset.addSeries(series);
	}
/*
	private void changeXYSeries(XYMultipleSeriesDataset dataset) {

		XYSeries series = new XYSeries(title, 0);
		int iPushTime = Integer.parseInt(app.mPushTime);
		int iHoldTime = Integer.parseInt(app.mHoldTime);
		int iResetTime = Integer.parseInt(app.mResetTime);
		int iSleepTime = Integer.parseInt(app.mSleepTime);

		int totalTime = 1000 * 2 + 1; // ((iPushTime + iHoldTime + iResetTime +
										// iSleepTime)*2)+1; //为了保持长度，所以乘以2

		double firstValue = yValue[totalTime / 2];
		for (int i = 0; i < xValue.length - 1; i++) {
			// 依次移动
			yValue[i] = yValue[i + 1];
		}
		yValue[xValue.length - 1] = firstValue;

		double[] xV = xValue; // X轴根据总时间确定数组长度，范围 4~10s
		double[] yV = yValue; // 根据4个时间确定数组的值
		int seriesLength = xV.length;
		for (int i = 0; i < seriesLength; i++) {
			series.add(xV[i], yV[i]);
		}
		dataset.addSeries(series);
	}*/
}
